package com.employees.action;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Hibernate;

import com.employees.bean.Employee;
import com.employees.bean.EmployeeBase;
import com.employees.bean.Employeeinfo;
import com.employees.bean.Employeeskill;
import com.employees.dao.EmployeeDao;
import com.employees.service.EmployeeService;

public class EmployeeEdit extends Action {

	private EmployeeService employeeService;
	
	private EmployeeDao employeeDao;
	

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
//		String SkillId=request.getParameter("SkillId");
//		String InfoId=request.getParameter("InfoId");
//		String employeeNo=request.getParameter("employeeNo");
//		String employeeName=request.getParameter("employeeName");
//		String employeeBirthday=request.getParameter("employeeBirthday");
//		SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd");  //譬ｼ蠑丞喧譌ｶ髣ｴ
//	    Date sDate=(Date) sdf.parse(employeeBirthday); //譬ｼ蠑丞喧startDate 蠑�ｧ区慮髣ｴ荳ｺ Date邀ｻ蝙�
//		String employeeSex=request.getParameter("employeeSex");
//		String employeeDepartment=request.getParameter("employeeDepartment");
//		String employeePhoto=request.getParameter("");
//		String[]  values=request.getParameterValues("employeeSkill");
//		String employeeSkill="";
//		for(int i=0; i<values.length;i++){
//			 employeeSkill+=values[i]+",";
//		}
//		//String employeeskill=request.getParameter("employeeSkill");
//		String employeeSkill1=employeeSkill.substring(0, employeeSkill.length()-1);
//		
		EmployeeBase base=(EmployeeBase) form;
//		int SkillId=base.getSkillId();
//		int InfoId=base.getInfoId();
		String SkillId=request.getParameter("SkillId");
		String InfoId=request.getParameter("InfoId");
		String employeeNo=base.getEmployeeNo();
		String employeeName=base.getEmployeeName();
		String employeeBirthday=base.getEmployeeBirthday();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  //譬ｼ蠑丞喧譌ｶ髣ｴ
	    Date sDate=(Date) sdf.parse(employeeBirthday); //譬ｼ蠑丞喧startDate 蠑�ｧ区慮髣ｴ荳ｺ Date邀ｻ蝙�
		String employeeSex=base.getEmployeeSex();
		String employeeDepartment=base.getEmployeeDepartment();
		InputStream stream=base.getPicture().getInputStream();
			
//		String  values=base.getEmployeeSkill();
		String[]  values=request.getParameterValues("employeeSkill");
		String employeeSkill="";
		for(int i=0; i<values.length;i++){
			 employeeSkill+=values[i]+",";
		}
//		String employeeskill=request.getParameter("employeeSkill");
		String employeeSkill1=employeeSkill.substring(0, employeeSkill.length()-1);
		System.out.println("----"+employeeSkill);
//		String employeeskill=request.getParameter("employeeSkill");
		
		Employee e=new Employee();
		e.setNo(employeeNo);
		e.setName(employeeName);
		
		Employeeinfo info=new Employeeinfo();
		
		info.setId(Integer.parseInt(InfoId));
		info.setBirth(sDate);
		info.setDepartment(employeeDepartment);
		info.setSex(employeeSex);
		info.setPhoto(Hibernate.createBlob(stream));
		info.setEmployee(e);
		
		Employeeskill skill=new Employeeskill();
		skill.setId(Integer.parseInt(SkillId));
		skill.setSkill(employeeSkill);
		skill.setEmployee(e);

		
		employeeService.updateEmployee(e);
		employeeService.updateEmployee(info);
		employeeService.updateEmployee(skill);
		return mapping.findForward("success");
	}

}
